/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package docview;

import java.awt.Dimension;
import java.awt.Font;
import java.lang.String;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import tableprocessor.CathegoryStaticTree;
import tableprocessor.CathegoryStaticTreeFactory;
import tableprocessor.ColumnPare;
import tableprocessor.Table;
import tableprocessor.PriceDAO;
/**
 *
 * @author Dema
 */
public class ViewNewDoc implements ViewDoc, CathegoryEventListener{

    protected DocTable doc;
    protected DocTable resultDoc;
    //protected BindWindow bindWindow;
    protected Vocabulary vocabulary;
    protected CathegoryView cathegory;
    protected DocTextArea text;
    protected DocTableManager manager ;
    protected ResultDocManager rd_manager;
    protected JButton toDBloadButton;
    protected PriceSessionStore storage = new PriceSessionStore();

    protected TableAction action = new TableAction();

    protected Set<DefaultMutableTreeNode> visitCat = new HashSet<DefaultMutableTreeNode>();

    public ViewNewDoc(DocViewManager _m)
    {
       doc = new DocTable(IObserver.Sender.PRICE);
       resultDoc = new DocTable(IObserver.Sender.RESULT);
       //bindWindow = new BindWindow();
       vocabulary = new Vocabulary();
       cathegory = new CathegoryView();
       //******************* commented by Gleb **************
       //text = new DocTextArea(1170,100);
       text = new DocTextArea();
       _m.addNewVocabulary(vocabulary);
      // manager = new DocTableManager(doc,storage);
       rd_manager = new ResultDocManager(resultDoc,storage,this);
       cathegory.registerEventListener(rd_manager);
       
       cathegory.registerEventListener(this);
       cathegory.registerEventListener(manager);
       doc.getColorTable().addMouseListener(manager);

       if (resultDoc.getToolBar()!=null)
       {
            toDBloadButton = new JButton("Загрузить в БД");
            resultDoc.getToolBar().add(toDBloadButton);
            toDBloadButton.addMouseListener(rd_manager);
       }

       action.setTrackTable(doc, IObserver.Sender.PRICE);
       action.setTrackTable(resultDoc, IObserver.Sender.RESULT);
       resultDoc.addButtonActionListener(action);
       doc.addSelectionListner(action);

       resultDoc.getColorTable().appendDeleteListener(rd_manager);

    }
    public Iterator<DefaultMutableTreeNode> getVisitedCathegories()
    {
        return visitCat.iterator();
    }
    public JPanel viewDocument(Iterator<Table> it) {

        doc.fill(it);
       // resultDoc.addColumn("1");
        resultDoc.addNewDataRow(new Vector(1));
        doc.setMinimumSize(new Dimension(250,550));
        doc.setPreferredSize(new Dimension(500,550));
        resultDoc.setMinimumSize(new Dimension(250,550));
        resultDoc.setPreferredSize(new Dimension(500,550));
        //bindWindow.setMinimumSize(new Dimension (230,550));
        //bindWindow.setPreferredSize(new Dimension(230,550));
        //bindWindow.setMaximumSize(new Dimension(300,Short.MAX_VALUE));
        cathegory.setMinimumSize(new Dimension(170,550));
        cathegory.setPreferredSize(new Dimension(170,550));
        cathegory.setMaximumSize(new Dimension(170,Short.MAX_VALUE));
        text.setMinimumSize(new Dimension(1170,100));
        text.setPreferredSize(new Dimension(1170,100));
        text.setMaximumSize(new Dimension(1170,100));
        //////////////////////////////////////////////////////////////
        /*bindWindow.addListener((IObserver) doc);
        bindWindow.addListener((IObserver) resultDoc);
        bindWindow.addListener((IObserver) vocabulary);
        vocabulary.addListener((IObserver) doc);
        vocabulary.addListener((IObserver) resultDoc);
        doc.addListener(vocabulary);
        doc.addListener(bindWindow);
        resultDoc.addListener(vocabulary);
        resultDoc.addListener(bindWindow);*/
        cathegory.setListener(resultDoc);
        doc.setListener(text);
        text.setListener(doc);


        //////////////////////////////////////////////////////////////
        JPanel horizontalPanel = new JPanel();
        JPanel verticalPanel = new JPanel();
        horizontalPanel.setLayout(new BoxLayout(horizontalPanel,BoxLayout.LINE_AXIS));
        verticalPanel.setLayout(new BoxLayout(verticalPanel,BoxLayout.Y_AXIS));
        horizontalPanel.add(doc);
        //panel.add(bindWindow);
        horizontalPanel.add(resultDoc);
        horizontalPanel.add(cathegory);
        verticalPanel.add(horizontalPanel);
        verticalPanel.add(text);


        return verticalPanel;

    }

    public void selectCathegoryEvent(DefaultMutableTreeNode node)
    {
        visitCat.add(node);
        CathegoryStaticTree cst = CathegoryStaticTreeFactory.getInstance();
        if (cst.isHasColumns(node))
        {
            Iterator<ColumnPare> iter = cst.getColumnsForCathegory(node);
            if (iter!=null)
            {
                resultDoc.clearTable();
                while (iter.hasNext())
                {
                    ColumnPare pare = iter.next();
                    resultDoc.addColumn(pare.getDbcol().getName());
                }
                Iterator<PriceSessionStoreObject> itero = storage.getContentByCathegory(node);
                int row = storage.getRowCountForCathegory(node);
                DefaultTableModel model = (DefaultTableModel) resultDoc.getColorTable().getModel();
                
                PriceDAO pdao = CathegoryStaticTreeFactory.getPriceDAO();
                int dbrows = pdao.getRowsInCathegory(node);
                model.setNumRows(row+dbrows+1);
                for (int i = 0 ; i < dbrows; i++)
                {
                    Iterator<String> siter = pdao.getRowByCathegory(node, i+1);
                    int ccounter = 0;
                    while (siter.hasNext())
                    {
                        resultDoc.getColorTable().setValueAt(siter.next(),i,ccounter);
                        ccounter++;
                    }
                }
                while (itero.hasNext())
                {
                    PriceSessionStoreObject obj = itero.next();
                    resultDoc.getColorTable().setValueAt(obj.getValue(), obj.getRow(), obj.getCol());
                }
            }
        }
    }
};
